package com.prac.redis.service;

import com.prac.redis.controller.TestTrans;
import com.prac.redis.mapper.UserMapper;
import com.prac.redis.pojo.User;
import org.springframework.aop.framework.AopContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author hxg
 * @version 1.0
 * @date 2022/5/8 21:36
 */
//todo 验证事务的各种失效
@Service
//@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
public class TransFail {
    @Resource
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    public void testA() {
        int id = new Random().nextInt(1000);
        User user = new User(id, "A", 22);
        userMapper.insert(user);
        testB();
    }

    @Transactional(rollbackFor = Exception.class)
    public void testB() {
        int id = new Random().nextInt(1000);
        User user = new User(id, "B", 22);
        if ("B".equals(user.getName())) {
            throw new RuntimeException("异常发生");
        }
        userMapper.insert(user);
    }
}
